home *** CD-ROM | disk | FTP | other *** search
- Path: svnews.ubinet.ubs.com!ubszh!ian.johnston@ubs.com
- From: ian.johnston@ubs.com (Ian Johnston (by ubsswop))
- Newsgroups: comp.lang.c++
- Subject: Re: Urgent help - pointers to functions
- Date: 1 Apr 1996 11:05:31 GMT
- Organization: UBS
- Distribution: world
- Message-ID: <4jod9r$ka@ubszh.fh.zh.ubs.com>
- References: <internews46B6FAA4E6@argonet.co.uk>
- NNTP-Posting-Host: nol2179.fh.zh.ubs.com
-
- In article <internews46B6FAA4E6@argonet.co.uk>, Charlotte Tomlinson <eeyore@argonet.co.uk> writes:
- |> I have implemented a template doubly linked list, the backbones of which
- |> is as follows:
- |>
-
- [...]
-
- |> Then I have a class, called fuzzyset, which uses this dll as a container
- |> for fuzzyel instances:
- |>
- |> //fuzzyset.h
- |> /*snip*/
- |>
- |> class fuzzyset {
- |> private:
- |> dllist<fuzzyel> members;
- |>
- |> public:
- |> fuzzyset() {}
- |> fuzzyset(const fuzzyset&);
- |> fuzzyset(const fuzzyel&);
- |> /*other functions and operators*/
- |> };
- |>
- |>
- |> What I would like to do is, within dllist, to implement a function forEach
- |> that take a pointer to a function as one argument, and somehow the rest of
- |> the arguments required by that function, and carries that function out
- |> over each element of the list.
- |>
- |> One example of what I need to do is as follows. In my main program (where
- |> fuzzyset is included), I need to be able to call a function that prints
- |> every element of the 'members' list in any called instance of a fuzzyset.
- |>
- |> I need the forEach function to be quite generic, because I will have other
- |> uses for it later.
-
- It's probably best here to use a "function object", that is, an object
- which represents a function.
-
- For example:
-
- template <class T>
- struct Function
- {
- virtual void operator ()(T const &item) const = 0;
- };
-
-
- template <class T>
- void dllist<T>::forEach(Function<T> const &func)
- {
- node<T> *node;
-
- for (node = head; node != 0; node = node->getnext())
- func(node->getinfo());
- }
-
-
- Now you can create derived classes of Function to do specific things:
-
-
- template <class T>
- struct ItemPrinter : public Function<T>
- {
- virtual void operator()(T const &item) const
- {
- cout << item << endl;
- }
- };
-
-
-
- // Somewhere in fuzzyset:
-
- ItemPrinter<fuzzyel> printer;
- members.forEach(printer);
-
-
- Or, even more compactly (and this is why the operator () is const):
-
- members.forEach(ItemPrinter<fuzzyel>());
-
-
-
- Your derived class of Function can include data members as, effectively,
- extra parameters you would have passed to the forEach function.
-
- For example, to change each item:
-
-
- template <class T>
- struct Changer : public Function<T>
- {
- Changer(T const &item)
- : value(item)
- {
- }
-
- virtual void operator ()(T const &item) const
- {
- item = value;
- }
-
- T const &value;
- };
-
-
-
- fuzzyel new_value;
- Changer func(new_value);
-
- members.forEach(func);
-
-
- Ian
-
-
-
-
-
-